<?php

namespace app\index\controller;

use app\admin\library\Auth;
use app\admin\model\AdminLog;
use app\common\controller\Frontend;
use EasyWeChat\Factory;
use app\admin\model\Admin;
use think\Cache;
use think\Db;
use think\Hook;
class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\base\Qr;
    }

    /**
     * 后台扫码登录
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\Exception
     */
    public function  ad(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);//serverip port
        $redis->select(1);
        for($i=0;$i<15000;$i++){

                $a=$redis->LPUSH('click',rand(1000,5000));
                var_dump($a);

        }


    }

    public function index()
    {
        if($code = $this->request->request('code'))
        {
           
            $app = Factory::work(Admin::UserConfig());
            $token = $app->access_token->getToken();//获取企业微信token
            $code = $this->request->request('code');
            $state = $this->request->request('state');
            $user = \fast\Http::get($url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$token['access_token']}&code={$code}");
            $user = json_decode($user,true);
           // $user['UserId'] = "GaoZhenYu";
            if(isset($user['UserId']))
            {
                $AuthLogin = new Auth();
                $AuthLogin->IndexLogin($user['UserId']);
              
                // if($state)
                // {
                //     $url = Cache::get($state);
                //     $this->redirect($url);
                // }
               AdminLog::setTitle('扫码登录');
               AdminLog::record();
               Hook::listen("admin_login_init", $this->request);
               $this->redirect($this->request->domain().'/admin/index/index.html');
            }
            $this->error('登录失败,请联系管理员！','/');
        }

        return $this->view->fetch();
    }

    public function mobileAdminLogin()
    {
        if($code = $this->request->request('code'))
        {
            $app = Factory::work(Admin::UserConfig());
            $token = $app->access_token->getToken();//获取企业微信token
            $code = $this->request->request('code');
            $user = \fast\Http::get($url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$token['access_token']}&code={$code}");
            $user = json_decode($user,true);
            if(isset($user['UserId']))
            {
                $AuthLogin = new Auth();
                $AuthLogin->IndexLogin($user['UserId']);
                AdminLog::setTitle('手机登录');
                AdminLog::record();
                Hook::listen("admin_login_init", $this->request);
                if($this->request->request('url') == 'http://oa.fc62.com/admin/agent/info/mobileIndex'){
                  $this->redirect('/admin/agent/info/mobileIndex');
                }else{
                  $this->redirect($this->request->domain().'/admin/index/index.html');
                }
            }
            $this->error('登录失败,请联系管理员！','/');
        }
        echo "手机登录";exit;
    }
     /**
     * 二维码展示
     * @return [type] [description]
     */
    public function qr($id){
      $row = $this->model->get($id);
      if (!$row)
        $this->error(__('No Results were found'));
         $result = $row->allowField(true)->setInc("qr_num",1);
        $this->view->assign("row", $row);
        return $this->fetch();
    }

}
